// 公告设置
<template>
  <div>

    <el-row class="bread-nav">
      <ul class="bread-nav-title">
        <span>公告设置</span>
      </ul>
      <ul class="bread-nav-title-right">
        <button @click="dialogFormVisible=!dialogFormVisible">
          <img src="../../../../static/agent/img/添加.png" height="20"/>
          添加公告
        </button>
      </ul>
    </el-row>

    <el-dialog title="添加公告" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="公告类型" :label-width="formLabelWidth" prop="top">
          <el-radio v-model="form.top" :label="0">非置顶公告</el-radio>
          <el-radio v-model="form.top" :label="1">置顶公告</el-radio>
        </el-form-item>
        <el-form-item label="公告内容" :label-width="formLabelWidth" prop="nContent">
          <el-input type="textarea"
                    :rows="4"
                    placeholder="请输入内容"
                    v-model="form.nContent"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNotice">确 定</el-button>
      </div>
    </el-dialog>

    <div class="content">
      <table class="myTable">
        <thead>
        <tr>
          <th>
            发布时间
          </th>
          <th>
            公告内容
          </th>
          <th>
            公告类型
          </th>
          <th>
            功能
          </th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item,index) in noticesList_data" :key="index" class="myTable-tr">
          <td>
            {{item.cTime}}
          </td>
          <td>
            {{item.content}}
          </td>
          <td>
            {{item.is_top === '1'?'置顶公告':'普通公告'}}
          </td>
          <td>
            <button @click="deleteNotice(item.id)">
              删除
            </button>
          </td>
        </tr>
        </tbody>
      </table>
    </div>

    <el-row class="noticeFooter">
      <ul class="noticeFooter-left">共{{noticesList_pageInfo.totalCount}}条公告 共{{pageNums.length}}页</ul>
      <ul class="noticeFooter-right">
        <button @click="getNoticesList(1)">首页</button>
        <button @click="getNoticesList(--currtPage)">前一页</button>
        <span>【</span>
        <button v-for="item in pageNums" :key="item" :class="item === currtPage?'noticeFooterSelected':''" @click="getNoticesList(item)">
          {{item}}
        </button>
        <span>】</span>
        <button @click="getNoticesList(++currtPage)">后一页</button>
        <button @click="getNoticesList(lastPage)">末页</button>
      </ul>
    </el-row>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        noticesList_data: '', // 公告内容
        noticesList_pageInfo: '', // 公告信息
        lastPage: 1, // 最后一页
        currtPage: 1, // 当前页
        dialogFormVisible: false, // 新增公告弹窗
        formLabelWidth: '100',
        form: {
          top: 0, // 是否是置顶公告0为非置顶，1为置顶
          nContent: '', // 公告内容
          type: 1, // 必填，1为普通公告，2为弹窗公告
        },
        rules: {
          top: [
            { required: true, message: '请选择公告类型', trigger: 'change' },
          ],
          nContent: [
            { required: true, message: '请填写公告内容', trigger: 'blur' },
          ],
        },
      }
    },
    computed: {
      pageNums() {
        const num = Math.ceil(parseInt(this.noticesList_pageInfo.totalCount) / parseInt(this.noticesList_pageInfo.pageSize))
        this.lastPage = num
        const ArrayNum = []
        for (let i = 1; i <= num; i++) {
          ArrayNum.push(i)
        }
        return ArrayNum
      },
    },
    created() {
      this.getNoticesList(this.currtPage)
    },
    methods: {
      // 添加公告 AddNotice
      addNotice() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            this.$store.dispatch('AddNotice', this.form)
              .then(response => {
                this.$message({
                  type: 'success',
                  message: response.message,
                })
                this.getNoticesList()
                this.dialogFormVisible = false
              })
          } else {
            return false
          }
        })
      },
      // 删除公告
      deleteNotice(value) {
        if (window.confirm('你确定要删除该公告么？')) {
          this.$store.dispatch('DeleteNotice', { id: value })
            .then(response => {
              this.$message({
                type: 'success',
                message: response.message,
              })
              this.getNoticesList()
            })
          return true
        } else {
          return false
        }
      },
      // 获取公告信息
      getNoticesList(value) {
        if (value < 0) {
          value = 1
        }
        if (value > this.lastPage) {
          value = this.lastPage
        }
        this.$store.dispatch('GetNoticesList', value)
          .then(response => {
            if (response.data === undefined) {
              this.$message({
                type: 'success',
                message: response.message,
              })
            } else {
              this.currtPage = value
              this.noticesList_data = response.data
              this.noticesList_pageInfo = response.pageinfo
            }
          })
      },
    },
  }
</script>

<style scoped>
  .bread-nav {
    height: 30px;
    font-size: 13px;
    line-height: 30px;
    background: #ECEFF2;
    border-bottom: 1px solid #d0dff2;
    padding: 0 0 0 10px;
    overflow: hidden;
  }

  .bread-nav-title {
    color: #386399;
    font-weight: bold;
    float: left;
    background: url("../../../../static/agent/img/tb.png") no-repeat left center;
    padding-left: 20px;
  }

  .bread-nav-title-right{
    float:right;
  }
  .bread-nav-title-right button{
    border: 0;
    background-color: rgba(0,0,0,0);
    height:30px;
    line-height: 30px;
    color:red;
    font-weight: bold;
  }
  .bread-nav-title-right button img{
    padding: 0;
    margin-top: -4px;
    vertical-align: middle;
    display: inline-block;
  }

  .content {
    padding: 10px;
  }

  .myTable {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
  }

  .myTable th {
    box-shadow: inset 0px -14px 19px 2px #CAE8F6;
    height: 25px;
    text-align: center;
    color: #131950;
    font-weight: bold;
    line-height: 25px;
    font-size: 14px;
    padding-left: 10px;
    padding-right: 10px;
    border: 1px solid #B9C2CB;
    margin: 0;
  }

  .myTable td {
    height: 25px;
    text-align: center;
    color: #131950;
    line-height: 25px;
    font-size: 14px;
    padding: 5px;
    border: 1px solid #B9C2CB;
  }

  .myTable-tr:hover {
    background-color: #CAE8F6;
  }

  .noticeFooter{
    background-color: #EEEEEE;
    margin: 0 5px;
  }
  .noticeFooter-left{
    float: left;
  }
  .noticeFooter-right{
    float: right;
  }
  .noticeFooter-right button{
    border:0;
    background-color: rgba(0,0,0,0);
    color: blue;
    cursor: pointer;
  }

  .noticeFooter-right button:hover {
    color: red;
    text-decoration: underline;
  }
  .noticeFooterSelected{
    color:#ccc !important;
  }
</style>

